#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>

//MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
//{

//}

MyPushButton::MyPushButton(QString normalImg, QString pressImg):normalImgPath(normalImg), pressImgPath(pressImg)
{
    QPixmap pix;
    bool ret = pix.load(normalImg);
    if(!ret) {
        qDebug()<< "图片加载失败："<< normalImg;
        return;
    }

    setFixedSize(pix.width(), pix.height());

    // 设置不规划图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    // 设置图标
    this->setIcon(pix);

    // 设置图标大小
    this->setIconSize({pix.width(), pix.height()});
}

void MyPushButton::zoom1()
{
    // 创建动画对象
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");

    // 设置动画时间间隔
    animation->setDuration(200);

    // 起始位置
    animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
    // 结束位置
    animation->setEndValue(QRect(this->x(), this->y()+10, this->width(), this->height()));
    // 设置曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}


void MyPushButton::zoom2()
{
    // 创建动画对象
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");

    // 设置动画时间间隔
    animation->setDuration(200);

    // 起始位置
    animation->setStartValue(QRect(this->x(), this->y()+10, this->width(), this->height()));
    // 结束位置
    animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));
    // 设置曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    if(pressImgPath.isEmpty())return QPushButton::mousePressEvent(e);
    QPixmap pix;
    bool ret = pix.load(pressImgPath);
    if(!ret) {
        qDebug()<< "图片加载失败："<< pressImgPath;
        return;
    }

    setFixedSize(pix.width(), pix.height());

    // 设置不规划图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    // 设置图标
    this->setIcon(pix);

    // 设置图标大小
    this->setIconSize({pix.width(), pix.height()});
    return QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(normalImgPath.isEmpty())QPushButton::mouseReleaseEvent(e);

    QPixmap pix;
    bool ret = pix.load(normalImgPath);
    if(!ret) {
        qDebug()<< "图片加载失败："<< normalImgPath;
        return;
    }

    setFixedSize(pix.width(), pix.height());

    // 设置不规划图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    // 设置图标
    this->setIcon(pix);

    // 设置图标大小
    this->setIconSize({pix.width(), pix.height()});
    QPushButton::mouseReleaseEvent(e);
}

